package com.mdoya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdoya.domain.Employee;
import com.mdoya.exception.BusinessExceptiion;
import com.mdoya.mapper.EmployeeMapper;
import com.mdoya.query.EmployeeQuery;
import com.mdoya.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@Scope("singleton")
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     *  员工登录
     * @param employee
     * @return
     */
    @Override
    public Employee login(Employee employee) {
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(employee.getUsername()),Employee::getUsername,employee.getUsername());
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(employee.getPassword()),Employee::getPassword,password);
        lambdaQueryWrapper.eq(Employee::getStatus,1);
        Employee emp = employeeMapper.selectOne(lambdaQueryWrapper);
        return emp;
    }

    /**
     *  查询所有员工
     * @return
     */
    @Override
    public IPage<Employee> selectPage(EmployeeQuery employeeQuery) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(employeeQuery.getName()),Employee::getName,employeeQuery.getName());
        IPage<Employee> employeePage = new Page<>(employeeQuery.getPage(),employeeQuery.getPageSize());
        return employeeMapper.selectPage(employeePage, queryWrapper);
    }

    /**
     *  添加员工
     * @param employee
     * @return
     */
    @Override
    public Integer insert(Employee employee,Long empId) {
        log.info("添加员工: {}",employee.toString());
        // 数据库设计username用户名唯一约束 Unique   1. 数据库唯一约束   2. 先查询用户名   3. 为了用户体验度在用户输入时候就应该直接发请求到后台去校验用户名是否可用
       /* LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(employee.getUsername() != null,Employee::getUsername,employee.getUsername());
        Employee emp = employeeMapper.selectOne(lambdaQueryWrapper);
        if (emp != null) throw new BusinessExceptiion(-1,"用户已存在");*/
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        // 初始化密码 默认值
        System.err.println("添加员工实现类：" + employee);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        System.err.println("添加员工实现类：" + employee);
        return employeeMapper.insert(employee);
    }

    /**
     * 根据id查询员工信息
     *
     * @param id 员工id
     * @return
     */
    @Override
    public Employee selectById(String id) {
        if (id == null) new BusinessExceptiion(-1,"没有找到该员工");
        return employeeMapper.selectById(id);
    }

    /**
     *  修改员工  问题  如果本想修改信息  没有输入框数据  想了想不修改  不小心点击了提交 那么这个时候 数据库被修改为空  所以需要vo对象接受请求参数 校验空数据则用原有的数据
     * @param employee 被员工员工对象
     * @param empId       操作人id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Integer updateById(Employee employee, Long empId) {
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        return employeeMapper.updateById(employee);
    }
}
